import { formatTreeData, isExternalLink, eachTreeData } from "./core";
function pathIsAdd(path, data) {
  let isAdd = false;
  eachTreeData(data, (item) => {
    if (path === item.path) {
      isAdd = true;
      return false;
    }
  });
  return isAdd;
}
function camelCase(str) {
  const val = str.replace(/[-|/](\w)/g, (_, c) => c ? c.toUpperCase() : "");
  return val.charAt(0).toUpperCase() + val.slice(1);
}
function createIframeComponent(name, url) {
  return {
    name,
    render(createElement) {
      return createElement("iframe", {
        attrs: {
          src: url
        },
        class: "ele-admin-iframe"
      });
    }
  };
}
function formatMenus(data, childField = "children") {
  let homePath;
  let homeTitle;
  const menus = formatTreeData(data, (item) => {
    var _a;
    const meta = typeof item.meta === "string" ? JSON.parse(item.meta) : item.meta;
    const path = (meta == null ? void 0 : meta.fullPath) || item.fullPath || item.path;
    const component = ((_a = item.component) == null ? void 0 : _a.startsWith("/")) ? item.component.substring(1) : item.component;
    const temp = Object.assign({}, item);
    temp[childField] = void 0;
    const menu = {
      path,
      component,
      name: item.name,
      redirect: item.redirect,
      meta: Object.assign({
        name: item.name,
        path: item.path,
        fullPath: item.fullPath,
        title: item.title,
        icon: item.icon,
        hide: item.hide,
        active: item.active,
        hideFooter: item.hideFooter,
        hideSidebar: item.hideSidebar,
        tabUnique: item.tabUnique,
        closable: item.closable,
        color: item.color,
        data: temp
      }, meta)
    };
    const children = item[childField];
    if (!(children == null ? void 0 : children.length)) {
      if (!homePath && path && !isExternalLink(path)) {
        homePath = path;
        homeTitle = menu.meta.title;
        menu.meta.isHome = true;
      }
    } else {
      const childPath = children[0].path || children[0].fullPath;
      if (childPath) {
        if (!menu.redirect) {
          menu.redirect = childPath;
        }
        if (!menu.path) {
          menu.path = childPath.substring(0, childPath.lastIndexOf("/"));
        }
      }
    }
    if (!path) {
      console.error("\u83DC\u5355path\u4E0D\u80FD\u4E3A\u7A7A\u4E14\u8981\u552F\u4E00: ", item);
      return;
    }
    return menu;
  }, childField);
  return { menus, homePath, homeTitle };
}
function menuToRoutes(menus, getComponent, added, redirectPath, redirectComponent, parentPath) {
  if (!(menus == null ? void 0 : menus.length)) {
    return;
  }
  const routes = [];
  const addedRoutes = added ? [...added] : [];
  menus.forEach((item) => {
    const route = menuToRoute(item, getComponent, addedRoutes, redirectPath, redirectComponent);
    if (route) {
      routes.push(route);
    }
  });
  if (routes.length && redirectPath && redirectComponent) {
    const temp = {
      path: (parentPath != null ? parentPath : "") + redirectPath + "/:path*",
      meta: { hideFooter: true }
    };
    routes.push(menuToRoute(temp, () => redirectComponent, []));
  }
  return routes;
}
function menuToRoute(menu, getComponent, added, redirectPath, redirectComponent) {
  const meta = Object.assign({}, menu.meta);
  const p = meta.path || menu.path;
  const path = (p == null ? void 0 : p.includes("?")) ? p.substring(0, p.indexOf("?")) : p;
  if (path && !isExternalLink(path) && !pathIsAdd(path, added)) {
    const name = menu.name || meta.name || camelCase(path);
    let component;
    if (menu.component && isExternalLink(menu.component)) {
      component = createIframeComponent(name, menu.component);
      meta.iframe = menu.component;
      meta.hideFooter = true;
    } else {
      component = getComponent(menu.component, menu, name);
    }
    added.push({ path });
    return {
      name,
      path,
      meta,
      component,
      redirect: menu.redirect,
      children: menuToRoutes(menu.children, getComponent, added, redirectPath, redirectComponent, path)
    };
  }
}
export { createIframeComponent, formatMenus, menuToRoute, menuToRoutes };
